package com.eendtech.bootpage;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.eendtech.bootpage.fragment_home.HomeOne;
import com.eendtech.bootpage.fragment_home.HomeThree;
import com.eendtech.bootpage.fragment_home.HomeTwo;

import java.util.ArrayList;
import java.util.List;

public class HomeActivity extends AppCompatActivity {
    private ViewPager vp_home ;
    private List<Fragment> viewList ;
    private LinearLayout tab1,tab2,tab3;
    private ImageView img_home,img_house,img_my;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        initData();
        initView();
        initTab();
        initViewPager();

    }

    /**
     * 取得组件
     */
    private void initView (){
        vp_home = (ViewPager) findViewById(R.id.vp_Home);
        tab1 = (LinearLayout)findViewById(R.id.tab_1);
        tab2 = (LinearLayout)findViewById(R.id.tab_2);
        tab3 = (LinearLayout)findViewById(R.id.tab_3);
        img_home = (ImageView)findViewById(R.id.img_home);
        img_house = (ImageView)findViewById(R.id.img_house);
        img_my = (ImageView)findViewById(R.id.img_my);
    }
    private void initViewPager (){
       vp_home.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
           @Override
           public Fragment getItem(int position) {
               return viewList.get(position);
           }

           @Override
           public int getCount() {
               return viewList.size();
           }

       });
       vp_home.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
           @Override
           public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

           }

           @Override
           public void onPageSelected(int position) {
               //进行ViewPager关联底部导航
               switch (position){
                   case 0:
                       img_home.setImageResource(R.drawable.grade_check);
                       img_house.setImageResource(R.drawable.kqselect_normal);
                       img_my.setImageResource(R.drawable.my_normal);
                       break;
                   case 1:
                       img_home.setImageResource(R.drawable.grade_normal);
                       img_house.setImageResource(R.drawable.kqselect_check);
                       img_my.setImageResource(R.drawable.my_normal);
                       break;
                   case 2:
                       img_home.setImageResource(R.drawable.grade_normal);
                       img_house.setImageResource(R.drawable.kqselect_normal);
                       img_my.setImageResource(R.drawable.my_check);
                       break;
               }
           }

           @Override
           public void onPageScrollStateChanged(int state) {

           }
       });
    }

    /**
     * 初始化底部导航栏
     */
    private void initTab (){
        img_home.setImageResource(R.drawable.grade_check);
        tab1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                vp_home.setCurrentItem(0);
            }
        });
        tab2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                vp_home.setCurrentItem(1);
            }
        });
        tab3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                vp_home.setCurrentItem(2);
            }
        });
    }
    /**
     * 初始化数据
     */
    private void initData(){
        viewList = new ArrayList<Fragment>();
        viewList.add(new HomeOne());
        viewList.add(new HomeTwo());
        viewList.add(new HomeThree());
    }


}
